package com.xuexiang.xuidemo.view;



import ohos.agp.animation.AnimatorValue;
import ohos.agp.components.Component;
import ohos.agp.render.Arc;
import ohos.agp.render.Canvas;
import ohos.agp.render.Paint;
import ohos.agp.utils.RectFloat;
import ohos.app.Context;
import ohos.eventhandler.EventHandler;
import ohos.eventhandler.EventRunner;
import ohos.hiviewdfx.HiLog;
import ohos.hiviewdfx.HiLogLabel;

import java.util.ArrayList;

/**
 * Created by Jack on 2015/10/16.
 */
public class BallClipRotateIndicator extends AVLoadingIndicatorView {
    static final HiLogLabel LABEL = new HiLogLabel(HiLog.LOG_APP, 0x0000008, "BallClipRotateIndicator");

    float scaleFloat=1,degrees;
    //Canvas canvas;
    Arc mArc;
    RectFloat rectF;
    //Paint paint;

    //EventRunner runner ;
    //private EventHandler mEventHandler;
//    Runnable task;
    Component.DrawTask taskdraw;
    float vdraw=1.0f;
    public BallClipRotateIndicator(Context context) {
        super(context);
        //runner = EventRunner.getMainEventRunner();
       // mEventHandler = new EventHandler(runner);
//        task = new Runnable() {
//            @Override
//            public void run() {
//                // 待执行的操作，由开发者定义
//                mArc.setArc(-45, 90, false);
//                canvas.drawArc(rectF, mArc, paint);
//                taskdraw = (component, canvas) -> {
//                    setPaint();
//                    setBound();
//                    draw(canvas,getPaint());
//                };
//                addDrawTask(taskdraw);
//            }
//        };
         taskdraw = (component, canvas) -> {
            setPaint();
            setBound();
            draw(canvas,getPaint());
        };
        //setLayoutRefreshedListener(this::onRefreshed);
        addDrawTask(taskdraw);
    }
    /*@Override
    protected void onRefreshed(Component component) {

    }*/

    @Override
    public void addDrawTask(Component.DrawTask drawTask){
        super.addDrawTask(drawTask);
        drawTask.onDraw(this, mCanvasForTaskOverContent);
    }

    @Override
    public void draw(Canvas canvas, Paint paint) {
        //this.canvas= canvas;
        //this.paint=paint;
        canvas.rotate(degrees,getWidth()/2f,getHeight()/2f);

        paint.setStyle(Paint.Style.STROKE_STYLE);
        paint.setStrokeWidth(15);


        float circleSpacing=12;
        float x = getWidth()/ 2f;
        float y= getHeight() / 2f;
        canvas.translate(x, y);
        canvas.scale(scaleFloat, scaleFloat);
//        vdraw=(300*vdraw-30)/300;
        rectF=new RectFloat(-x+circleSpacing,-y+circleSpacing,0+x-circleSpacing,0+y-circleSpacing);
        /*mArc=new Arc( 0*vdraw, 360*vdraw, false);*/
        mArc=new Arc( 20*vdraw, 600*vdraw, false);
        canvas.drawArc(rectF, mArc, paint);
    }

    @Override
    public ArrayList<AnimatorValue> onCreateAnimators() {
        ArrayList<AnimatorValue> animators=new ArrayList<>();
        AnimatorValue scaleAnim=new AnimatorValue();
        scaleAnim.setDuration(750);
        scaleAnim.setLoopedCount(-1);
        addUpdateListener(scaleAnim,new AnimatorValue.ValueUpdateListener() {
            @Override
            public void onUpdate(AnimatorValue animatorValue, float v) {

                scaleFloat = scaleHs(v);
                invalidate();
            }
        });
        AnimatorValue rotateAnim=new AnimatorValue();
        rotateAnim.setDuration(1500);
        rotateAnim.setLoopedCount(-1);
        addUpdateListener(rotateAnim,new AnimatorValue.ValueUpdateListener() {
            @Override
            public void onUpdate(AnimatorValue animatorValue, float v) {
//                canvas.drawArc(rectF, new Arc( -45, 270, false), paint);
                HiLog.error(LABEL, "addUpdateListener="+v+"");
//              mArc=new Arc( -45, 270*v, false);
//                mArc.setArc(-45, 90, false);
//                canvas.drawArc(rectF, mArc, paint);
//                mEventHandler.postTask(task, 0, EventHandler.Priority.IMMEDIATE); // 延时0ms后立即处理
                vdraw=v;
                if(vdraw>0.5f){
                    vdraw=1-vdraw;
                }else{
                    vdraw=vdraw;
                }
                degrees = v*600;
                invalidate();
            }
        });
//        animators.add(scaleAnim);
        animators.add(rotateAnim);
        return animators;
    }
    @Override
    public void invalidate() {
      super.invalidate();

    }
    protected float scaleHs(float v){
        float res=0;
        if (v<= (float) 1/3)
            res= (float) (-1.2*v+1);
        else if (v> (float) 1/3 && v<= (float) 2/3)
            res= (float) (-0.3*v+0.7);
        else if(v> (float) 2/3)
            res= (float) (1.5*v-0.5);
        return res;
    }

}
